var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/Attachment/components/LabelSelect.tsx
var LabelSelect_exports = {};
__export(LabelSelect_exports, {
  LabelSelect: () => LabelSelect
});
module.exports = __toCommonJS(LabelSelect_exports);
var import_iup_core = require("@newgrand/iup-core");
var import_antd = require("antd");
var import_react = require("react");
var import_baseComponent = require("../../../baseComponent");
var import_util = require("../../../util");
var import_service = require("../service");
var defaultWidth = 150;
function LabelSelect(props) {
  const { labelConfig, fileId, onChangeCallback } = props;
  const { options, multiple, style, defaultValue, disabled, value: propsValue, ...others } = labelConfig;
  const [open, setOpen] = (0, import_react.useState)(false);
  const [updating, setUpdating] = (0, import_react.useState)(false);
  const [value, setValue] = (0, import_react.useState)([]);
  const preValue = (0, import_react.useMemo)(() => {
    const v = propsValue || defaultValue;
    return Array.isArray(v) ? v : [v];
  }, [propsValue, defaultValue]);
  (0, import_react.useEffect)(() => {
    setValue(preValue);
  }, [preValue]);
  const handleLabelChange = (0, import_iup_core.useRefCallback)((label) => {
    setUpdating(true);
    const params = {
      fileId,
      label
    };
    (0, import_service.saveAttachLabel)(params).then((res) => {
      if ((res == null ? void 0 : res.code) === 0) {
        onChangeCallback && onChangeCallback();
      } else {
        setValue(defaultValue);
      }
    }).finally(() => {
      setUpdating(false);
    });
  });
  const newProps = {
    options,
    multiple: !!multiple,
    disabled,
    style: { width: defaultWidth, maxHeight: "100%", ...style },
    ...others
  };
  if (newProps.multiple) {
    newProps.maxTagCount = newProps.hasOwnProperty("maxTagCount") ? newProps.maxTagCount : "responsive";
    newProps.maxTagPlaceholder = (omittedValues) => {
      const len = omittedValues == null ? void 0 : omittedValues.length;
      if (len) {
        const tipTitle = /* @__PURE__ */ React.createElement(
          "div",
          {
            style: { padding: "6px 8px", wordBreak: "break-all", whiteSpace: "normal" },
            onClick: (e) => e.stopPropagation(),
            onMouseDown: (e) => e.stopPropagation()
          },
          omittedValues.map((v) => v.label).join(", ")
        );
        return /* @__PURE__ */ React.createElement(import_antd.Tooltip, { overlayInnerStyle: { padding: 0 }, title: tipTitle, mouseEnterDelay: 0.2 }, /* @__PURE__ */ React.createElement("span", { style: { display: "inline-flex", alignItems: "center", maxWidth: "100%" } }, /* @__PURE__ */ React.createElement("span", { className: "nowrap", style: { flex: 1 } }, omittedValues[0].label), len > 1 ? `,  + ${len - 1} ...` : ``));
      }
      return null;
    };
  }
  return /* @__PURE__ */ React.createElement(
    import_baseComponent.Select,
    {
      ...newProps,
      open,
      loading: newProps.loading || updating,
      value,
      onDropdownVisibleChange: (_open) => {
        setOpen(_open);
        if (!_open && preValue.join("") !== value.join("")) {
          handleLabelChange(value);
        }
      },
      onChange: (v) => {
        const values = import_util.iup.isArray(value) ? v.map((op) => op.value) : [v.value];
        if (!open) {
          handleLabelChange(values);
        }
        setValue(values);
      }
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LabelSelect
});
